package ModelLayer;
import java.util.*;

public class LendCon
{
    private ArrayList<Lendings> lendings;
    private ArrayList<Copy> allCopies;
    private static LendCon instance;
    
    /**
     * Constructor of objects of class LendCon
     */
    public LendCon()
    {
        lendings = new ArrayList<Lendings>();  
        allCopies = new ArrayList<Copy>();
    }
    
    /**
     * Creates new lend by calling Lendings contructor with parameters.
     * Then it stores the object in the container. 
     */
    public void createLending(Person newPerson, Copy newCopy, String newDate, String newPeriod)
    {
        Lendings lending = new Lendings(newPerson, newCopy, newDate, newPeriod);
        addLending(lending);
    }
    
    /**
     * Singelton method for working with only one instance of LendCon
     */
    public static LendCon getInstance()
    {
       if(instance == null)
           {
               instance = new LendCon();
           }
       return instance;
    }
    
    /**
     * Adds Lendings objects to LendCon
     */
    public void addLending(Lendings newLending)
    {
        lendings.add(newLending);
    }
    
    /**
     * Removes Lendings objects from LendCon
     */
    private void deleteLending(int id)
    {
        lendings.remove(getLending(id));
    }
    
    /**
     * Returns a Lendings objects from an id search
     */
    public Lendings getLending(long id)
    {
        int index = 0;
        boolean found = false;
        Lendings tempLend = null;
        while(index < lendings.size() && !found)
        {
            tempLend = lendings.get(index);
            if(tempLend.getId() == id)
            {
                found = true;
            }
            
            else {
                index++;
            }
        }
        
        if(found)
        {
            return lendings.get(index);
        }
        
        else {
            return null;
        }
    }
    
    /**
     * Returns a Lendings object from a serial nr search
     */
    public Lendings getLendingSerialNr(long serialNr)
    {
        int index = 0;
        boolean found = false;
        Lendings tempLend = null;
        while(index < lendings.size() && !found)
        {
            tempLend = lendings.get(index);
            if(tempLend.getCopy().getSerialNr() == serialNr)
            {
                found = true;
            }
            
            else {
                index++;
            }
        }
        
        if(found)
        {
            return lendings.get(index);
        }
        
        else {
            return null;
        }
    }
    
    /**
     * Mutator method 
     */
    public void updateLending(Lendings l, String date, String period)
    {
        l.updateLending(date, period);   
    }
    
    /**
     * Returns information about instance is avaiable for loan or not
     */
    public boolean isOut(Copy copy)
    {
        boolean found = false;
        int i= 0;
        while(i < lendings.size() && !found)
        {
            Lendings l = lendings.get(i);
            if(l.getCopy().getSerialNr() == copy.getSerialNr())
            {
                found = true;
            }
            
            else {
                i++;
            }
        }
        
        return found;
    }
    
    /**
     * Prints long information about Lendings objects from LendCon
     */
    public void getLendCon()
    {
        for(Lendings lend: lendings)
        {
            System.out.println("ID: " + lend.getId() + "\n" + "Date: " + lend.getDate() + "\n" + "Period: " + lend.getPeriod());
        }
    }
    
    /**
     * Returns an ArrayList containing Lendings objects 
     */
    public ArrayList<Lendings> listAllLendings()
    {
        return lendings;   
    }
    
    /**
     * Prints long information about Lendings objects from LendCon
     */
    public void printAllLendings()
    {
        for (Lendings lend : lendings)
        {
            System.out.println("Id: " + lend.getId() + " \t Period: " + lend.getPeriod());
        }
       
    }
    
    /**
     * Returns an ArrayList containing Copy objects
     */
    public ArrayList<Copy> allCopies()
    {
        return allCopies();
    }
    
    /**
     * Returns an instance of Copy
     */
    public Copy getCopy()
    {
        return LendCon.getInstance().getCopy();
    }
    
}